package user

import (
	"app/api/middleware"
	"app/internal/app"
	"app/internal/model/supplier"
	"app/internal/pkg"
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"time"
)

// 发送邮箱验证处理层
func SendCode(ctx *gin.Context) {
	// 获取请求的model层
	var user supplier.SendCodeRequest
	//进行数据绑定
	err := ctx.ShouldBindJSON(&user)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": err.Error(),
		})
		return
	}

	//获取随机生成的code
	code := pkg.RandowNum()
	//验证码存在数据库
	result := app.RedisClient.Set(context.TODO(), user.Email, code, time.Minute*60) //60分钟过期
	_, err = result.Result()
	if err != nil { //数据库储存失败
		log.Println(err)
	}

	//邮箱发送信息的内容
	msg := fmt.Sprintf("supplier: %s,验证码有效期为1个小时。", code)
	//发送邮箱
	err = pkg.SendEmail(user.Email, msg, app.Viper.GetString("email.username"), app.Viper.GetString("email.password"), app.Viper.GetString("email.host"), app.Viper.GetString("email.port"))
	if err != nil { //邮箱发送失败
		log.Println(err)
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": err,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": user.Email,
	})
}

// 商家用户实现注册
func Register(ctx *gin.Context) {
	// 获取请求的model层
	var user supplier.RegisterRequest
	//进行数据绑定
	err := ctx.ShouldBindJSON(&user)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": err.Error(),
		})
		return
	}

	//从redis判断用户验证码是否正确
	result, err := app.RedisClient.Get(context.Background(), user.Email).Result()
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": err.Error(),
		})
		return
	}
	if result != user.Code {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "验证码已过期",
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": user.Email,
	})
}

// 用户账号密码登陆
func LoginPass(ctx *gin.Context) {
	// 获取请求的model层
	var user supplier.LoginPass
	//进行数据绑定
	err := ctx.ShouldBindJSON(&user)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": err.Error(),
		})
		return
	}

	/*
		业务处理
	*/

	token, err := pkg.GenToken(user.Email, middleware.SupplierUserExpireDuration, middleware.SupplierUserSecretKey)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": err.Error(),
		})
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to login",
		"token":   token,
		"email":   user.Email,
	})
}

// 用户验证码登陆
func LoginCode(ctx *gin.Context) {
	// 获取请求的model层
	var body supplier.LoginCode
	//进行数据绑定
	err := ctx.ShouldBindJSON(&body)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": err.Error(),
		})
		return
	}

	//这里首先判断验证码是否正确
	result, err := app.RedisClient.Get(context.Background(), body.Email).Result()
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": err.Error(),
		})
		return
	}

	if result != body.Code {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "验证码已经失效",
		})
		return
	}
	/*
		这里调用链码查询商家是否存在
	*/

	//验证码正确 并且商家账号确实存在，生成token
	token, err := pkg.GenToken(body.Email, middleware.SupplierUserExpireDuration, middleware.SupplierUserSecretKey)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": err.Error(),
		})
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "ok",
		"token":   token,
		"email":   body.Email,
	})

}

// 获取用户首页数据
func GetSupplierIndex(ctx *gin.Context) {
	//对请求数据进行json bind
	var body supplier.LoginCode
	err := ctx.ShouldBindJSON(&body)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to bing request data:" + err.Error(),
		})
		return
	}

	data := supplier.SupplierIndexData{
		DailyOrderNumber:                 120,
		DailySalesTotal:                  "12000.50",
		PreviousSalesTotal:               "11000.40",
		CompletedOrder:                   90,
		ShippedOrder:                     80,
		PendingDeliveryConfirmationOrder: 10,
		CancelledOrder:                   5,
		PendingShipmentOrder:             20,
		OutOfStockRegistration:           15,
		ListedProduct:                    200,
		TightWarehouseInventory:          50,
		AllProducts:                      250,
		DailyBrowse:                      500,
		PreviousBrowse:                   450,
		MonthBrowse:                      15000,
		AllBrowse:                        50000,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success accept request,email:" + body.Email,
		"data":    data,
	})
}

// 搜索商品
func SupplierSearchGoods(ctx *gin.Context) {
	//绑定请求数据
	var data supplier.SupplierSearchGoods
	err := ctx.ShouldBindJSON(&data)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to bing request data:" + err.Error(),
		})
		return
	}

	goods := []supplier.Good{
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to search goods,email: " + data.Email + ",keysWords:" + data.KeyWords,
		"data":    goods,
	})
}

// 商品列表
func GetSupplierGoods(ctx *gin.Context) {
	//绑定请求数据
	var data supplier.SupplierSearchGoods
	err := ctx.ShouldBindJSON(&data)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to bing request data:" + err.Error(),
		})
		return
	}

	goods := []supplier.Good{
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
		{
			Id:     1,
			Name:   "Macbook 13",
			Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
			Price:  "9999",
			Stock:  1000,
			Unit:   "个",
			Status: 1,
		},
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to get goods,email:" + data.Email,
		"data":    goods,
	})
}

// 单个商品详情
func GetSupplierGoodsDetail(ctx *gin.Context) {
	//绑定请求数据
	var data supplier.GetSupplierGoodsDetailRequest
	err := ctx.ShouldBindJSON(&data)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to bing request data:" + err.Error(),
		})
		return
	}

	goods := supplier.Good{
		Id:     1,
		Name:   "Macbook 13",
		Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
		Desc:   "Macos 17包含所有可行的",
		Price:  "9999",
		Stock:  1000,
		Unit:   "个",
		Status: 1,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to search goods,email: ",
		"data":    goods,
	})
}

// 修改用户状态
func ModifySupplierGoodsStatus(ctx *gin.Context) {
	var data supplier.UpdateSupplierGoodsRequest
	err := ctx.ShouldBindJSON(&data)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to bing request data:" + err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to modify goods status.",
	})
}

// 添加商品
func AddSupplierGoods(ctx *gin.Context) {
	var data supplier.AddSupplierGoodsRequest
	err := ctx.ShouldBindJSON(&data)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to bing request data:" + err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to add goods.",
	})
}

// 删除商品
func DeleteSupplierGoods(ctx *gin.Context) {
	//绑定数据
	var data supplier.DeleteSupplierGoodsRequest
	err := ctx.ShouldBindJSON(&data)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to get data:" + err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to delete data",
	})
}

// 搜索订单
func SearchSupplierOrder(ctx *gin.Context) {
	//绑定数据
	var data supplier.SearchSupplierOrderRequest
	err := ctx.ShouldBindJSON(&data)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to get data:" + err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to search order",
		"data": supplier.Order{
			Id:         1,
			Supplier:   "2476358005@qq.com",
			User:       "golandcxy@gmail.com",
			Amount:     "1000",
			Status:     1,
			CreateTime: time.Now().Format("2006-01-02 15:04:05"),
		},
	})
}

// 获取订单信息
func GetSupplierOrderData(ctx *gin.Context) {
	//绑定数据
	var data supplier.SearchSupplierOrderRequest
	err := ctx.ShouldBindJSON(&data)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to get data:" + err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to get order:" + data.Email,
		"data": []supplier.Order{
			{
				Id:         1,
				Supplier:   "2476358005@qq.com",
				User:       "golandcxy@gmail.com",
				Amount:     "1000",
				Status:     1,
				CreateTime: time.Now().Format("2006-01-02 15:04:05"),
			},
			{
				Id:         1,
				Supplier:   "2476358005@qq.com",
				User:       "golandcxy@gmail.com",
				Amount:     "1000",
				Status:     0,
				CreateTime: time.Now().Format("2006-01-02 15:04:05"),
			},
			{
				Id:         1,
				Supplier:   "2476358005@qq.com",
				User:       "golandcxy@gmail.com",
				Amount:     "1000",
				Status:     1,
				CreateTime: time.Now().Format("2006-01-02 15:04:05"),
			},
			{
				Id:         1,
				Supplier:   "2476358005@qq.com",
				User:       "golandcxy@gmail.com",
				Amount:     "1000",
				Status:     1,
				CreateTime: time.Now().Format("2006-01-02 15:04:05"),
			},
			{
				Id:         1,
				Supplier:   "2476358005@qq.com",
				User:       "golandcxy@gmail.com",
				Amount:     "1000",
				Status:     1,
				CreateTime: time.Now().Format("2006-01-02 15:04:05"),
			},
			{
				Id:         1,
				Supplier:   "2476358005@qq.com",
				User:       "golandcxy@gmail.com",
				Amount:     "1000",
				Status:     1,
				CreateTime: time.Now().Format("2006-01-02 15:04:05"),
			},
			{
				Id:         1,
				Supplier:   "2476358005@qq.com",
				User:       "golandcxy@gmail.com",
				Amount:     "1000",
				Status:     1,
				CreateTime: time.Now().Format("2006-01-02 15:04:05"),
			},
			{
				Id:         1,
				Supplier:   "2476358005@qq.com",
				User:       "golandcxy@gmail.com",
				Amount:     "1000",
				Status:     1,
				CreateTime: time.Now().Format("2006-01-02 15:04:05"),
			},
		},
	})
}

// 查看订单详情
func GetSupplierOrderDetail(ctx *gin.Context) {
	//绑定数据
	var data supplier.SearchSupplierOrderRequest
	err := ctx.ShouldBindJSON(&data)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to get data:" + err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to search order",
		"data": supplier.Order{
			Id:          1,
			Supplier:    data.Email,
			User:        "golandcxy@gmail.com",
			Amount:      "1000",
			Status:      1,
			CreateTime:  time.Now().Format("2006-01-02 15:04:05"),
			Invoice:     "lsr",
			Logistics:   1,
			LogisticsId: "asdadwdadwwff",
			GoodsInfo: []supplier.Good{
				{
					Id:     1,
					Name:   "Macbook 13",
					Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
					Desc:   "Macos 17包含所有可行的",
					Price:  "9999",
					Stock:  1000,
					Unit:   "个",
					Status: 1,
				},
				{
					Id:     1,
					Name:   "Macbook 13",
					Pic:    "https://daisyui.com/images/stock/photo-1534528741775-53994a69daeb.jpg",
					Desc:   "Macos 17包含所有可行的",
					Price:  "9999",
					Stock:  1000,
					Unit:   "个",
					Status: 1,
				},
			},
			OperateInfo: []supplier.Operate{{
				User:        "supplier",
				OperateTime: time.Now().Format("2006-01-02 15:04:05"),
				OrderStatus: 1,
				Remark:      "备注",
			},
				{
					User:        "supplier",
					OperateTime: time.Now().Format("2006-01-02 15:04:05"),
					OrderStatus: 1,
					Remark:      "备注",
				}},
			ReceiptInfo: supplier.UserAddress{
				Name:        "lsr",
				PhoneNumber: "1318722211",
				Address:     "湖南",
			},
		},
	})
}

// 实现商品修改
func ModifySupplierGoods(ctx *gin.Context) {
	//绑定数据
	var data supplier.ModifySupplierGoodsRequest
	err := ctx.ShouldBindJSON(&data)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to get data:" + err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to modify goods:" + data.Name,
	})
}

// 实现订单发货
func SupplierDeliverOrder(ctx *gin.Context) {
	//绑定数据
	var data supplier.SupplierDeliverOrderRequest
	err := ctx.ShouldBindJSON(&data)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to get data:" + err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to accept data:" + data.DeliverId,
	})
}

// 切换头像
func SupplierChangeHead(ctx *gin.Context) {
	//绑定数据
	var data supplier.SupplierChangeHeadRequest
	err := ctx.ShouldBindJSON(&data)
	if err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    500,
			"message": "failed to get data:" + err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "success to accept data:" + data.Pic,
	})
}
